﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Text;
using QModel;

namespace QLibrary.Helper
{
    public class ParamHelper
    {
        public static string BuildParas(Dictionary<string, object> paras)
        {
            if (paras.Count <= 0 || paras == null)
            {
                return null;
            }

            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<string, object> param in paras)
            {
                string key = param.Key;
                object value = param.Value;
                Type type = value.GetType();
                sb.Append(key);
                sb.Append("=");
                if ("String".Equals(type.Name))
                {
                    sb.Append(value);
                }
                else if ("Int32".Equals(type.Name))
                {
                    sb.Append(Convert.ToInt32(value));
                }
                else if ("Int64".Equals(type.Name))
                {
                    sb.Append(Convert.ToInt64(value));
                }
                sb.Append("&");
            }
            return sb.ToString().Substring(0, sb.Length - 1);
        }

        public static Dictionary<string, object> BuildDictionary(string paras)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            if (!string.IsNullOrEmpty(paras))
            {
                string[] andColl = paras.Split('&');
                foreach (string str in andColl)
                {
                    string[] equColl = str.Split('=');
                    dic.Add(equColl[0], equColl[1]);
                }
            }
            return dic;
        }

        public static T GetParam<T>(string key, string body)
        {
            if (body.Contains("?"))
            {
                body = body.Substring(body.IndexOf("?") + 1);
            }
            object value;
            Dictionary<string, object> dic = new Dictionary<string, object>();
            string[] andColl = body.Split('&');
            foreach (string str in andColl)
            {
                string[] equColl = str.Split('=');
                dic.Add(equColl[0], equColl[1]);
            }
            dic.TryGetValue(key, out value);
            return (T)value;
        }

        public static string GetRRSig(Dictionary<string,object> dicParas)
        {
            List<APIParameter> listParas = new List<APIParameter>();
            foreach (KeyValuePair<string, object> entry in dicParas)
            {
                string key = entry.Key;
                string value = entry.Value.ToString();
                APIParameter p = new APIParameter(key, value);
                listParas.Add(p);
            }
            StringBuilder sb = new StringBuilder();
            listParas.Sort(new ParameterComparer());
            foreach (APIParameter parameter in listParas)
            {
                sb.AppendFormat("{0}={1}", parameter.Name, parameter.Value.ToString());
            }
            sb.Append(Constants.SecretKey);
            return MD5Encrypt(sb.ToString());
        }

        public static string MD5Encrypt(string source)
        {
            return MD5CryptoServiceProvider.GetMd5String(source);
        }
    }
}
